package ft.ess.util;

import java.net.*;
import java.util.*;

public class NetUtil
{
//	public static void main(String[] args) {
//		getLocalHost();
//	}
	
	/**
	 * 取得本机所有网卡的所有网络地址<br />
	 * 应该是标准的 java 方法
	 * @return
	 */
	public static List<Map<String,String>> getLocalHost()
	{
		
		List<Map<String,String>> al = null;
		
		try
		{
			Enumeration<NetworkInterface> e1 = NetworkInterface.getNetworkInterfaces();
			
			al = new ArrayList<Map<String,String>>();
			
			while(e1.hasMoreElements())
			{
				NetworkInterface ni = (NetworkInterface)e1.nextElement();
				
				String niName = ni.getName();
				//System.out.println("ni.getName()="+ni.getName());
				//System.out.print(":");
				
				Enumeration<InetAddress> e2 = ni.getInetAddresses();
				while(e2.hasMoreElements())
				{
					InetAddress ia = e2.nextElement();
					
					//Class cls = ia.getClass();
					//System.out.println("ia class name = "+cls.getName());
					if(ia instanceof Inet4Address)
					{
						String iaAddr = ia.getHostAddress();
						//System.out.println("ia.getHostAddress()="+ia.getHostAddress()); // IP 地址
						//System.out.println("ia.getHostName()="+ia.getHostName()); // 主机名，操作系统安装后设置有关
						
						if(niName!=null && niName.length()>0 && iaAddr!=null && iaAddr.length()>0 && !"127.0.0.1".equals(iaAddr))
						{
							Map<String,String> m = new HashMap<String,String>();
							m.put(niName, iaAddr);
							al.add(m);
							
							//System.out.println(niName+"|"+iaAddr);
						}
					}
					
				}
				
				
			}
			
		}
		catch (SocketException e)
		{
			e.printStackTrace();
		}
		return al;
	}
}
